package com.z.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.z.common.web.base.BaseController;
import com.z.common.web.vo.request.PageVo;
import com.z.common.web.vo.response.Result;
import com.z.sys.entity.Permission;
import com.z.sys.service.IPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.*;

/**
 * 权限资源管理Controller
 *
 * @author z
 * @date 2022-11-07
 */
@RestController
@RequestMapping("/system/permission")
public class PermissionController extends BaseController {

    private String prefix = "system/permission";

    @Autowired
    private IPermissionService permissionService;

    @GetMapping("/main")
    public ModelAndView main() {
        return jumpPage(prefix + "/main");
    }

    @GetMapping("/main_tree")
    public ModelAndView main_tree() {
        return jumpPage(prefix + "/main_tree");
    }
    /**
     * 查询权限资源管理列表
     */
    @ResponseBody
    @GetMapping("/data")
    public Result data(@ModelAttribute Permission permission, PageVo pageVo) {
        PageInfo<Permission> pageInfo = permissionService.selectPermissionPage(permission, pageVo);
        return Result.table(pageInfo);
    }

    /**
     * 查询权限资源管理列表
     */
    @ResponseBody
    @GetMapping("/list")
    public Result list(@ModelAttribute Permission permission) {

        List<Permission> list = permissionService.selectPermissionList(permission);
        return Result.table(list);
    }

    /**
     * 新增权限资源管理
     */
    @GetMapping("/add")
    public ModelAndView add() {
        return jumpPage(prefix + "/add");
    }

    /**
     * 新增权限资源管理
     */
    @ResponseBody
    @PostMapping("/save")
    public Result save(@RequestBody Permission permission) {
        permission.setIcon("fa " +permission.getIcon());
        return Result.decide(permissionService.save(permission));
    }

    /**
     * 修改权限资源管理
     */
    @GetMapping("/edit")
    public ModelAndView edit(Long id, ModelMap map) {
        Permission permission =permissionService.getById(id);
        map.put("permission", permission);
        return jumpPage(prefix + "/edit");
    }

    /**
     * 修改权限资源管理
     */
    @ResponseBody
    @PutMapping("/update")
    public Result update(@RequestBody Permission permission) {
        permission.setIcon("fa " + permission.getIcon());
        return Result.decide(permissionService.updateById(permission));
    }

    /**
     * 删除权限资源管理
     */
    @ResponseBody
    @DeleteMapping("/batchRemove")
    public Result batchRemove(String ids) {
        return Result.decide(permissionService.removeByIds(Arrays.asList(ids.split(","))));
    }

    /**
     * 删除权限资源管理
     */
    @ResponseBody
    @DeleteMapping("/remove/{id}")
    public Result remove(@PathVariable("id") Long id) {

        return Result.decide(permissionService.removeByIdAndPid(id));
    }

    /**
     * Describe: 获取父级权限选择数据
     * Param sysPower
     * Return Result
     */
    @GetMapping("/power/selectParent")
    public Result selectParent() {
        List<Permission> list = new ArrayList<>();
        Permission basePower = new Permission();
        basePower.setName("root");
        basePower.setId(0L);
        basePower.setParentId(-1L);
        List<Permission> permissions = permissionService.list(new QueryWrapper<Permission>().eq("del", 0).eq("parent_id","0").or().eq("type", "1"));
        // basePower.setChildren(permissions);
        list.add(basePower);
        list.addAll(permissions);
        return Result.table(200, list);
    }
}
